Note: There are often multiple ways to answer each question.

Load the ggplot2 and fueleconomy packages, as well as the vehicles dataset.

library(ggplot2)
library(fueleconomy)
data(vehicles)
  1. Make a scatterplot of hwy vs. cty.
ggplot(vehicles, aes(x = cty, y = hwy)) +
    geom_point()

  1. Convert the cyl column to a factor.
vehicles$cyl <- factor(vehicles$cyl)
  1. Modify the plot from Qn 1 such that the color of the dot represents cyl value. Also, change the color scale to “YlOrRd”.
ggplot(vehicles, aes(x = cty, y = hwy, col = cyl)) +
    geom_point() +
    scale_color_brewer(palette = "YlOrRd")
## Warning: Removed 58 rows containing missing values (geom_point).

Notice how the NAs got removed!

  1. There is a lot of overplotting in the plot above. Remove the color scale and modify the previous plot so that alpha = 0.1.
ggplot(vehicles, aes(x = cty, y = hwy, col = cyl)) +
    geom_point(alpha = 0.1)

  1. Make a histogram of year.
ggplot(vehicles, aes(x = year)) +
    geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  1. Make a histogram of year with just 5 bins.
ggplot(vehicles, aes(x = year)) +
    geom_histogram(bins = 5)

  1. For each value of cyl, make a violin plot of hwy values.
ggplot(vehicles, aes(x = cyl, y = hwy)) +
    geom_violin()

  1. For each value of cyl, make a boxplot of hwy values.
ggplot(vehicles, aes(x = cyl, y = hwy)) +
    geom_boxplot()

  1. Make a barplot to show how many cars of each type of fuel there are in the dataset. (Hint: Use the geom_bar geom.)
ggplot(vehicles, aes(x = fuel)) +
    geom_bar()

  1. Add a coord_flip() layer to the previous plot to make a horizontal barplot.
ggplot(vehicles, aes(x = fuel)) +
    geom_bar() +
    coord_flip()